package service;

import entity.Manager;
import entity.Operator;
import function.Druid;
import function.ManagerDaoImpl;
import function.OperatorDaoImpl;
import interfaces.OperatorServiceDao;
import tool.MyTool;
import tool.TSUtility;
import view.LogView;

import java.util.List;

public class OperatorServiceDaoImpl implements OperatorServiceDao {
    ManagerDaoImpl managerDao = ManagerDaoImpl.getManagerDao();
    OperatorDaoImpl operatorDao = OperatorDaoImpl.getOperatorDao();
    ManagerServiceImpl managerService=ManagerServiceImpl.getManagerService();
    private static OperatorServiceDaoImpl operatorService=null;
    private  OperatorServiceDaoImpl(){

    }
    public  static OperatorServiceDaoImpl getOperatorService(){
        if(operatorService==null)
            operatorService=new OperatorServiceDaoImpl();
        return operatorService;
    }


    @Override
    public void insert() throws Exception {
        System.out.println("请输入你要插入的操作员用户名（直接按确定退出）：");
        String name = MyTool.getSixString(true,"");
        if (name.equals("")) {
            System.out.println("退出成功");
            return;
        }
        System.out.println("请输入密码");
        String password = MyTool.getSixString(false,null);
        List<Manager> managers = managerDao.selectAll(Druid.getConnection());
        MyTool.printArr(managers);
        System.out.println("请选择管理员（序号）");
        Integer managerIndex = MyTool.getTypeIndex(managers.size());

        int managerId = 0;
        if (managerIndex == null) managerId = 3;
        else managerId = managers.get(managerIndex).getId();
        Operator operator = new Operator(name, password, managerId);
        System.out.println("是否添加(Y/N)?");
        char c = TSUtility.readConfirmSelection();
        if (c == 'N') return;
        boolean b = operatorDao.insertOperator(Druid.getConnection(), operator);
        if (b) {
            System.out.println("添加成功");
            Journal.record(LogView.getManager().getName(),"添加了操作员",name);
        }
        else System.out.println("添加失败");
    }

    @Override
    public void delete() throws Exception {
        List<Operator> operatorAll = getOperatorAll();
        System.out.println("请输入你要删除操作员的序号：（直接按确定退出）");
        Integer index = MyTool.getTypeIndex(operatorAll.size());
        if (index == null) {
            System.out.println("退出成功");
            return;
        }
        int id = operatorAll.get(index).getId();
        System.out.println("是否法删除（Y/N）?");
        char c = TSUtility.readConfirmSelection();
        if (c == 'N') return;
        Operator operator = operatorDao.selectById(Druid.getConnection(), id);
        boolean b = operatorDao.deleteOperator(Druid.getConnection(), id);
        if (b) {
            System.out.println("删除成功");
            Journal.record(LogView.getManager().getName(),"删除了操作员",operator.getName());
        }
        else System.out.println("删除失败");
    }

    @Override
    public void update() throws Exception {
        List<Operator> operatorAll = getOperatorAll();
        System.out.println("请选择你要修改操作员的序号（直接按确定退出）：");
        Integer index = MyTool.getTypeIndex(operatorAll.size());
        Integer id=operatorAll.get(index).getId();
        if (id == null) {
            System.out.println("退出成功");
            return;
        }
        while (true) {
            Operator operator = operatorDao.selectById(Druid.getConnection(), id);
            if(operator!=null){
                System.out.print("用户名（"+operator.getName()+"):");
                String name = MyTool.getSixString(true,operator.getName());
                System.out.println("密码（"+operator.getPassword()+"):");
                String password = MyTool.getSixString(true, operator.getPassword());
                System.out.println("管理员编号（"+operator.getManagerId()+")：");
                List<Manager> managers = managerService.selectAll();
                Integer managerId =MyTool.getTypeIndex(managers.size());
                if(managerId==null)managerId=operator.getManagerId();
                else managerId=managers.get(managerId).getId();
                System.out.println("是否修改？（Y/N）");
                char c = TSUtility.readConfirmSelection();
                if(c=='N')return;
                Operator operator1=new Operator(operator.getId(),name,password,managerId);
                boolean b = operatorDao.updateOperator(Druid.getConnection(), id, operator1);
                if(b) {
                    System.out.println("修改成功");
                    Journal.record(LogView.getManager().getName(),"修改了操作员",name,"的信息");
                }
                else System.out.println("修改失败");
                return;
            }else {
                System.out.println("操作员不存在，请查询选择：");
            }
        }

    }

    @Override
    public Operator getOperatorById() throws Exception {
        System.out.println("请输入你要查询的操作员的id（直接按确定退出）：");
        Integer id = MyTool.readInt();
        if (id == null) {
            System.out.println("退出成功");
            return null;
        }
        Operator operator = operatorDao.selectById(Druid.getConnection(), id);
        if (operator != null) {
            Journal.record(LogView.getManager().getName(),"通过id查询了操作员",operator.getName());
            MyTool.printOne(operator);
        }
        else System.out.println("没有相关结果");
        return operator;
    }

    @Override
    public List<Operator> getOperatorByManager() throws Exception {
        System.out.println("请输入管理员id（直接按确认退出）");
        Integer managerId = MyTool.readInt();
        if(managerId==null) {
            System.out.println("退出成功");
            return null;
        }
        List<Operator> operators = operatorDao.selectByManager(Druid.getConnection(),managerId);
        if(operators!=null){
            Journal.record(LogView.getManager().getName(),"通过管理员id查询了操作员的相关信息");
            MyTool.printArr(operators);
        }
        else System.out.println("没有相关结果");
        return operators;
    }

    @Override
    public List<Operator> getOperatorAll() throws Exception {
        List<Operator> operators = operatorDao.selectAll(Druid.getConnection());
        if(operators!=null){
            Journal.record(LogView.getManager().getName(),"通过id查询了操作员信息");
            MyTool.printArr(operators);
        }
        else System.out.println("还没有添加操作员");
        return operators;
    }

}
